from django.http import HttpResponseRedirect
import settings
from django.core.urlresolvers import reverse
from djangoopenid.utils import get_url_host

try:
    from functools import wraps
except ImportError:
    from django.utils.functional import wraps  # Python 2.3, 2.4 fallback.

def openid_login_required(view_func):
  def wrapped_func(request, *args, **kwargs):
    if not 'openiduser' in request.session: 
      return HttpResponseRedirect(settings.LOGIN_URL)
    return view_func(request, *args, **kwargs)
  return wraps(view_func)(wrapped_func)

def begin_development_environment(view_func):
  def wrapped_func(request, *args, **kwargs):
    if hasattr(settings, 'DEBUG') and settings.DEBUG:
      sufix = reverse('djangoopenid.views.complete', args=None)
      host = get_url_host(request)
      return_to = host + sufix
      redirectURL = return_to
      return HttpResponseRedirect(redirectURL)
    return view_func(request, *args, **kwargs)
  return wraps(view_func)(wrapped_func)

def complete_development_environment(view_func):
  def wrapped_func(request, *args, **kwargs):
    if hasattr(settings, 'DEBUG') and settings.DEBUG:
      request.session['openiduser'] = 'http://debug.devenvironment.localhost'
      return HttpResponseRedirect(settings.LOGIN_REDIRECT_URL)
    return view_func(request, *args, **kwargs)
  return wraps(view_func)(wrapped_func)
